package bavaria.hightech.banking.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 * ActionListener for the Specify Account Button. checks the account number for
 * validity and sets it as nr. to work with
 * 
 * @author Dietmar
 */
public class ListenerSpecifyAccountButton implements ActionListener {

	private JTextField accNrTextField;
	private JLabel labelReview;
	private GlobalVarsContainer cont;

	public ListenerSpecifyAccountButton(JTextField accNrTextField,
			JLabel labelReview, GlobalVarsContainer cont) {
		this.accNrTextField = accNrTextField;
		this.labelReview = labelReview;
		this.cont = cont;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		try {
			Integer selAccNr = Integer.parseInt(accNrTextField.getText());
			if (cont.getBank().isValidAccNr(selAccNr)) {
				cont.setSelAccNr(Integer.parseInt(accNrTextField.getText()));
				labelReview.setText("Selected account number is now: "
						+ cont.getSelAccNr() + "");
				cont.setStatusLabel("selecting account was successful");
			} else {
				labelReview
						.setText("You didn't specify an valid account number");
			}
		} catch (NumberFormatException e) {
			labelReview.setText("You didn't specify an valid account number");
		}
	}

}
